/**
 * Created by czw on 2017/7/15 0015.
 */
// 命名空间注册函数
var namespace = {
    reg: function (s) {
        var arr = s.split('.');
        var namespace = window;
        for (var i = 0, k = arr.length; i < k; i++) {
            if (typeof namespace[arr[i]] == 'undefined') {
                namespace[arr[i]] = {};
            }
            namespace = namespace[arr[i]];
        }
    },
    del: function (s) {
        var arr = s.split('.');
        var namespace = window;
        for (var i = 0, k = arr.length; i < k; i++) {
            if (typeof namespace[arr[i]] == 'undefined') {
                return;
            } else if (k == i + 1) {
                delete  namespace[arr[i]];
                return;
            } else {
                namespace = namespace[arr[i]];
            }
        }
    }
};
// // 先注册命名空间
// namespace.reg('config.admin.module.color');
//
// // 添加功能
// config.admin.module.color.changeBgColor = function(color){
//     document.body.style.background = color;
// };
//
// // 调用功能
// config.admin.module.color.changeBgColor('#F30');
//
//
// // 删除命名空间
// namespace.del('config.admin.module.color');
namespace.reg("baseJS");
baseJS = (function () {

    var layerOpenParameterFun = function () {
        var layerOpenParameter = {
            id: "layerOpen"
            , width: 800
            , height: 600
            , url: ""
            , type: 2
            , title: "窗口"
            , maxmin: true
            , isFull: false
            , isOutAnim: true
            , anim: 0
            , fixed: true
            , resize: false
            , successCallbackLayerOpenFun: null
            , endCallbackLayerOpenFun: null
        }
        return layerOpenParameter;
    }
    var ajaxParameter = {
        url: null
        , dataType: "json"
        , type: "post"
        , data: null
        , async: false
        , successCallback: null
        , errorCallback: null
        , beforeSend: function () {
            //请求前的处理
            ///	alert("请求前的处理..");
        },
        success: function (req) {
            if (ajaxParameter.successCallback != null && typeof(ajaxParameter.successCallback) == "function") {
                ajaxParameter.successCallback(req);
            } else {
                baseJS.layerMsg("操作成功");
            }
        },
        complete: function () {
            //请求完成的处理
            //alert("请求完成的处理..");
        },
        error: function () {
            //请求出错处理
            if (ajaxParameter.errorCallback != null && typeof(ajaxParameter.errorCallback) == "function") {
                ajaxParameter.errorCallback();
            } else {
                baseJS.layerMsg("ajaxRequest请求出错");
            }
        }
    }
    var baseLayerOpen = function (paramet) {
        var layerOpenParameter = layerOpenParameterFun();
        $.extend(layerOpenParameter, paramet);
        if (layerOpenParameter.type == 1) {
            baseJS.ajaxRequest({
                url: layerOpenParameter.url
                , dataType: "html"
                , success: function successCallback(data) {
                    layerOpenParameter.url = data;
                }
            });
        }
        var fullIndex = layer.open({
            type: layerOpenParameter.type,
            title: layerOpenParameter.title,
            id: layerOpenParameter.id,
            resize: layerOpenParameter.resize,
            maxmin: layerOpenParameter.maxmin,
            isOutAnim: layerOpenParameter.isOutAnim,
            area: [layerOpenParameter.width + "px", layerOpenParameter.height + "px"],
            content: layerOpenParameter.url,
            success: function (val, index) {
                if (layerOpenParameter.successCallbackLayerOpenFun != null && typeof(layerOpenParameter.successCallbackLayerOpenFun) == "function") {
                    layerOpenParameter.successCallbackLayerOpenFun(val, index);
                }
            },
            end: function () {
                layerOpenParameter.isFull = false;
                if (layerOpenParameter.endCallbackLayerOpenFun != null && typeof(layerOpenParameter.endCallbackLayerOpenFun) == "function") {
                    layerOpenParameter.successCallbackLayerOpenFun(val, index);
                }
            }
        });
        //全屏打开
        if (layerOpenParameter.isFull) {
            layer.full(fullIndex);
        }
    }

    var baseLayerOpenTop = function (paramet) {
        var layerOpenParameter = layerOpenParameterFun();
        $.extend(layerOpenParameter, paramet);
        if (layerOpenParameter.type == 1) {
            baseJS.ajaxRequest({
                url: layerOpenParameter.url
                , dataType: "html"
                , success: function successCallback(data) {
                    layerOpenParameter.url = data;
                }
            });
        }
        if (layerOpenParameter.isFull) {
            layerOpenParameter.width = "100%";
            layerOpenParameter.height = "100%";
        } else {
            layerOpenParameter.width += "px";
            layerOpenParameter.height += "px";
        }
        var fullIndex = layer.open({
            title: layerOpenParameter.title,
            type: layerOpenParameter.type,
            resize: layerOpenParameter.resize,
            maxmin: layerOpenParameter.maxmin,
            isOutAnim: layerOpenParameter.isOutAnim,
            area: [layerOpenParameter.width, layerOpenParameter.height],
            //content: '/backstage/menuManage/fontclass',
            content: layerOpenParameter.url,
            zIndex: layer.zIndex,
            success: function (layero, index) {
                layer.setTop(layero);
                if (layerOpenParameter.successCallbackLayerOpenFun != null && typeof(layerOpenParameter.successCallbackLayerOpenFun) == "function") {
                    layerOpenParameter.successCallbackLayerOpenFun(layero, index);
                }
            }
        });
        if (layerOpenParameter.isFull) {
            layer.full(fullIndex);
        }
        /*layui.use('layer', function(){
            var layer = layui.layer;
            var fullIndex = layer.open({
                type: layerOpenParameter.type,
                title: layerOpenParameter.title,
                id:layerOpenParameter.id,
                resize:layerOpenParameter.resize,
                maxmin: layerOpenParameter.maxmin,
                zIndex: layer.zIndex, //重点1
                area: [layerOpenParameter.width+"px", layerOpenParameter.height+"px"],
                content:layerOpenParameter.url,
                success:function (val,index) {
                    alert(val);
                    layer.setTop(val); //重点2
                    if( layerOpenParameter.successCallbackLayerOpenFun != null && typeof(layerOpenParameter.successCallbackLayerOpenFun) == "function"){
                        layerOpenParameter.successCallbackLayerOpenFun(val,index);
                    }
                },
                end:function () {
                    if( layerOpenParameter.endCallbackLayerOpenFun != null && typeof(layerOpenParameter.endCallbackLayerOpenFun) == "function"){
                        layerOpenParameter.successCallbackLayerOpenFun(val,index);
                    }
                }
            });
            //全屏打开
            if(layerOpenParameter.isFull){
                layer.full(fullIndex);
            }
        });*/
    }

    var layerOpenAle = function (content, title, successCallbackLayerOpenFun) {
        layer.open({
            content: content,
            title: title,
            yes: function (layero, index) {
                if (successCallbackLayerOpenFun != null && typeof(successCallbackLayerOpenFun) == "function") {
                    successCallbackLayerOpenFun(layero, index);
                }
            }
        });
    }

    /**
     * <p>如果空返回true,否则返回false</p>
     */
    var isEmpty = function (object) {
        if (typeof(object) == "undefined" || object == null || object == "") {
            return true;
        } else {
            return false;
        }
    }

    /**
     * <p>url：请求路径</p>
     * <p>reqDataType：请求数据返回数据类型默认json</p>
     * <p>isAsync：是否异步</p>
     * <p>datas：请求参数，格式：{参数名1：参数值1，参数名2：参数值2}</p>
     * <p>成功回调方法：successCallback</p>
     * <p>出错回调方法：errorCallback</p>
     */
    var ajaxRequest = function (paramet) {
        $.extend(ajaxParameter, paramet);
        if (baseJS.isEmpty(ajaxParameter.url)) {
            baseJS.layerMsg("url不能为空");
            return true;
        }
        $.ajax(ajaxParameter);

    }

    var ajaxRequestSsyncFalse = function (url,dadas) {
        var reqData = null;
        $.ajax({
            url: url,
            dataType: "json",
            async: false,
            data: dadas,
            type: "post",
            success: function (data) {
                reqData = data;
            },
            error: function () {
                baseJS.layerMsg("请求出错");
            }
        });
        return reqData;
    }

    var layerMsg = function (content, callback) {
        if (!baseJS.isEmpty(callback) && typeof(callback) == "function") {
            layer.msg(content, function () {
                callback();
            });
        } else {
            layer.msg(content, {time: 800});
        }
    }

    var bindingFormData = function (object, item) {
        $("#" + object + " input").each(function () {
            switch ($(this).attr("type")) {
                case 'text':
                    if (!baseJS.isEmpty(item[$(this).data().name])) {
                        $(this).val(item[$(this).data().name]);
                    }
                    break;
                case 'checkbox':
                    var itemVal;
                    if (!baseJS.isEmpty(item[$(this).data().name])) {
                        itemVal = item[$(this).data().name];
                    }
                    if ($(this).val() == itemVal) {
                        $(this).prop("checked", true);
                    }
                case 'radio':
                    var itemVal;
                    if (!baseJS.isEmpty(item[$(this).data().name])) {
                        itemVal = item[$(this).data().name];
                    }
                    if ($(this).val() == itemVal) {
                        $(this).prop("checked", true);
                    }
                    break;
            }
        });
        $("#" + object + " select").each(function () {
            var itemVal;
            if (!baseJS.isEmpty(item[$(this).data().name])) {
                itemVal = item[$(this).data().name];
            }
            if (!isNaN(itemVal)) {
                $(this).val(itemVal);
            } else if (!baseJS.isEmpty(itemVal)) {
                $(this).find("option[text='" + itemVal + "']").attr("selected", true);
            }
        })
        $("#" + object + " textarea").each(function () {
            if (!baseJS.isEmpty(item[$(this).data().name])) {
                $(this).val(item[$(this).data().name]);
            }
        })
    }

    return {
        baseLayerOpen: baseLayerOpen,
        ajaxRequest: ajaxRequest,
        ajaxRequestSsyncFalse: ajaxRequestSsyncFalse,
        isEmpty: isEmpty,
        layerMsg: layerMsg,
        bindingFormData: bindingFormData,
        baseLayerOpenTop: baseLayerOpenTop,
        layerOpenAle: layerOpenAle
    };

})();